package cn.e3mall.content.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.e3mall.common.jedis.JedisClient;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import cn.e3mall.common.utils.E3Result;
import cn.e3mall.content.service.ContentService;
import cn.e3mall.mapper.TbContentMapper;
import cn.e3mall.pojo.TbContent;
import cn.e3mall.pojo.TbContentExample;
import cn.e3mall.pojo.TbContentExample.Criteria;

/**
 * 内容管理Service
 * <p>Title: ContentServiceImpl</p>
 * <p>Description: </p>
 * <p>personal</p>
 *
 * @version 1.0
 */
@Service
public class ContentServiceImpl implements ContentService {
    private static final Logger LOG = LoggerFactory.getLogger( ContentServiceImpl.class );
    @Autowired
    private TbContentMapper contentMapper;
    @Autowired
    private JedisClient jedisClient;

    @Value("${redis.contentList}")
    private String CONTENT_LIST;

    @Override
    public E3Result addContent(TbContent content) {
        //将内容数据插入到内容表
        content.setCreated( new Date() );
        content.setUpdated( new Date() );
        //插入到数据库
        contentMapper.insert( content );
        jedisClient.hdel( CONTENT_LIST, String.valueOf( content.getCategoryId() ) );
        return E3Result.ok();
    }

    /**
     * 根据内容分类id查询内容列表
     * <p>Title: getContentListByCid</p>
     * <p>Description: </p>
     *
     * @param cid
     * @return
     * @see cn.e3mall.content.service.ContentService#getContentListByCid(long)
     */
    @Override
    public List<TbContent> getContentListByCid(long cid) {

        try {
            String hget = jedisClient.hget( CONTENT_LIST, String.valueOf( cid ) );
            if (StringUtils.isNotBlank( hget )) {
                List<TbContent> list = JSONObject.toJavaObject( JSONArray.parseArray( hget ), List.class );
                LOG.info( "查询到了缓存数据" );
                return list;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        TbContentExample example = new TbContentExample();
        Criteria criteria = example.createCriteria();
        //设置查询条件
        criteria.andCategoryIdEqualTo( cid );
        //执行查询
        List<TbContent> list = contentMapper.selectByExampleWithBLOBs( example );
        try {
            jedisClient.hset( CONTENT_LIST, String.valueOf( cid ), JSONArray.toJSONString( list ) );
            LOG.info( "插入了缓存数据" );
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public String save(Map<String, String> map) {
        return null;
    }

    @Override
    public E3Result updateContent(TbContent map) {
        map.setUpdated( new Date() );
        //插入到数据库
        contentMapper.updateByPrimaryKey( map );
        return E3Result.ok();
    }

}
